<?php
    class Tune_in extends CI_Model{

        public function __construct(){

            parent::__construct();
            $this->load->database();
        }

        //调宿人员查询方法
        public function tune_in_select($name, $jobNumber){
            if ($name == '' || $jobNumber == '') {
                $return = array('res' => '40002', 'msg' => '请输入必填字段' ,'data' => '');
                $result = json_encode($return);
            } else {
                // $where = array('name' => $name ,'jobNumber' => $jobNumber);
                // $select = array('department' ,'check_inWhether');
                // $this->db->select($select);
                // $this->db->where($where);
                // $query_dorm_livepeople = $this->db->get('dorm_livepeople');
                $query_dorm_livepeople = $this->db->get_where('dorm_livepeople' ,array('name' => $name ,'jobNumber' => $jobNumber));
                if($query_dorm_livepeople->num_rows()>0){
                    $return_su = array('res' => '40000', 'msg' => '查询成功', 'data' => $query_dorm_livepeople->row());
                    $result = json_encode($return_su);
                }else{
                        $return_fa = array('res' => '40001', 'msg' => '查询失败' ,'data' => '');
                        $result = json_encode($return_fa);
                    }
                }
            return $result;
        }

        //调换宿舍信息查询方法:取区域
        public function tune_in_area(){
            $select = array('area_number' ,'area_name' ,'area_location');
            $this->db->select($select);
            $query = $this->db->get('dorm_area');
            if($query){
                $return_su = array('res' => '40000', 'msg' => '查询成功', 'data' => $query->result());
                $result = json_encode($return_su);
            }else{
                $return_fa = array('res' => '40001', 'msg' => '查询失败');
                $result = json_encode($return_fa);
            }
            return $result;
        }

        //调换宿舍信息查询方法:取宿舍楼
        public function tune_in_dorm($area_number){
            if ($area_number == '') {
                $return = array('res' => '40002', 'msg' => '请输入必填字段' ,'data' => '');
                $result = json_encode($return);
            } else {
                //取此区域的所有宿舍楼
                $select = array('floor_number' ,'floor_name' ,'floor_location');
                $this->db->select($select);
                $this->db->where('area_number' ,$area_number);
                $query = $this->db->get('dorm_sslmanage');
                if ($query->num_rows() > 0) {
                    $return_su = array('res' => '40000', 'msg' => '查询成功', 'data' => $query->row());
                    $result = json_encode($return_su);
                } else {
                    $return_fa = array('res' => '40001', 'msg' => '查询失败');
                    $result = json_encode($return_fa);
                }
            }
            return $result;
        }

        //调换宿舍信息查询方法:取楼层
        public function tune_in_floor($area_number, $floor_number){
            if ($area_number == '' || $floor_number == '') {
                $return = array('res' => '40002', 'msg' => '请输入必填字段' ,'data' => '');
                $result = json_encode($return);
            } else {
                //取此宿舍楼的所有楼层
                // $where = array('area_number' => $area_number ,'floor_number' => $floor_number); 
                // $this->db->select('floorNumber');
                // $this->db->where($where);
                // $query = $this->db->get('dorm_sslmanage');
                // $result = $query->row();
                $arr = [];
                if($area_number = '1585' && $floor_number = '1'){
                    $arr = [3,4,5,6];
                }elseif($area_number = '1585' && $floor_number = '2'){
                    $arr = [1];
                }elseif($area_number = '333' && $floor_number = '3'){
                    $arr = [1,2,3];
                }elseif($area_number = '178' && $floor_number = '4'){
                    $arr = [1];
                }
                if(count($arr)>0){
                    $return_su = array('res' => '40000', 'msg' => '查询成功', 'data' => $arr);
                    $result = json_encode($return_su);
            
                }else{
                    $return_fa = array('res' => '40001', 'msg' => '查询失败');
                    $result = json_encode($return_fa);
                }
            }
            return $result;
        }

        //调换宿舍信息查询方法:取宿舍
        public function tune_in_room($area_number ,$floor_number ,$dorm_floor){
            if ($area_number == '' || $floor_number == '' || $dorm_floor == '') {
                $return = array('res' => '40002', 'msg' => '请输入必填字段' ,'data' => '');
                $result = json_encode($return);
            } else {
                //取此楼层的所有房间
                // $where = array('AR_number' => $AR_number ,'SSL_number' => $SSL_number ,'SS_floor' => $SS_floor ,);
                // $this->db->select('SS_number');
                // $this->db->where($where);
                $sj_dorm_roominfor = "area_number = '".$area_number."' AND floor_number = '".$floor_number."' AND dorm_floor = '".$dorm_floor."' AND remainNumber > 0";
                $sql_dorm_roominfor = "SELECT room_number FROM dorm_roominfor WHERE ".$sj_dorm_roominfor;
                $query = $this->db->query($sql_dorm_roominfor ,$sj_dorm_roominfor);
                if($query->num_rows()>0){
                    $return_su = array('res' => '40000', 'msg' => '查询成功', 'data' => $query->result());
                    $result = json_encode($return_su);
                }else{
                    $return_fa = array('res' => '40001', 'msg' => '查询失败' ,'data' => '');
                    $result = json_encode($return_fa);
                }
            }
            return $result;
        }

        //调换宿舍信息查询方法:取床位
        public function tune_in_bed($area_number ,$floor_number ,$room_number){
            if ($area_number == '' || $floor_number == '' || $room_number == '') {
                $return = array('res' => '40002', 'msg' => '请输入必填字段' ,'data' => '');
                $result = json_encode($return);
            } else {
                //取此宿舍的所有床位
                $where = array('area_number' => $area_number ,'floor_number' => $floor_number ,'room_number' => $room_number ,'check_inWhether' => 0);
                $this->db->select('bedNumber');
                $this->db->where($where);
                $query = $this->db->get('dorm_bed');
                if($query->num_rows()>0){
                    $return_su = array('res' => '40000', 'msg' => '查询成功', 'data' => $query->result());
                    $result = json_encode($return_su);
                }else{
                    $return_fa = array('res' => '40001', 'msg' => '查询失败' ,'data' => '');
                    $result = json_encode($return_fa);
                }
            }
            return $result;
        }

        // //调换宿舍信息查询方法：取柜子/判断床位
        // public function tune_in_isbed($AR_name, $SSL_name, $SS_floor, $RY_number, $RY_bed)
        // {
        //     if ($AR_name == '' || $SSL_name == '' || $SS_floor == '' || $RY_number == '' || $RY_bed == '') {
        //         $return = array('res' => '40002', 'msg' => '请输入必填字段');
        //         $result = json_encode($return);
        //     } else {
        //         $query = $this->db->get_where('dorm_sslmanage', array('AR_name' => $AR_name, 'SSL_name' => $SSL_name));
        //         $query1 = $this->db->get_where('dorm_livepeople', array('AR_number' => $query->row(0)->AR_number, 'SSL_number' => $query->row(0)->SSL_number, 'RY_number' => $RY_number, 'RY_bed' => $RY_bed));
        //         $query2 = $this->db->get_where('dorm_livepeople', array('AR_number' => $query->row(0)->AR_number, 'SSL_number' => $query->row(0)->SSL_number, 'RY_number' => $RY_number));
        //         if ($query1->num_rows() > 0) {
        //             $return_fa = array('res' => '40001', 'msg' => '所调换宿舍床位有人');
        //             $result = json_encode($return_fa);
        //         } else {
        //             $return_su = array('res' => '40000', 'msg' => '所选择床位无人居住，允许调换', 'data' => $RY_bed, $query2->row(0)->RY_wardrobe);
        //             $result = json_encode($return_su);
        //         }
        //     }
        //     return $result;
        // }

        // //调换宿舍信息查询方法：判断柜子
        // public function tune_in_wardrobe($RY_idcardnum, $AR_name, $SSL_name, $SS_floor, $RY_number, $RY_wardrobe)
        // {
        //     if ($RY_idcardnum == '' || $AR_name == '' || $SSL_name == '' || $SS_floor == '' || $RY_number == '') {
        //         $return = array('res' => '40002', 'msg' => '请输入必填字段');
        //         $result = json_encode($return);
        //     } else {
        //         $query = $this->db->get_where('dorm_sslmanage', array('AR_name' => $AR_name, 'SSL_name' => $SSL_name));
        //         $query1 = $this->db->get_where('dorm_livepeople', array('AR_number' => $query->row(0)->AR_number, 'SSL_number' => $query->row(0)->SSL_number, 'RY_number' => $RY_number, 'RY_wardrobe' => $RY_wardrobe));
        //         if ($query1->num_rows() > 0) {
        //             $return_fa = array('res' => '40001', 'msg' => '所选柜子有人使用');
        //             $result = json_encode($return_fa);
        //         } else {
        //             $return_su = array('res' => '40000', 'msg' => '所选柜子无人使用，可以使用', 'data' => $RY_wardrobe);
        //             $result = json_encode($return_su);
        //         }
        //     }
        //     return $result;
        // }

        // //调换宿舍方法
        // public function tune_inO($RY_idcardnum, $AR_name, $SSL_name, $SS_floor, $RY_number, $RY_bed, $RY_wardrobe, $SQ_BG_date)
        // {
        //     if ($RY_idcardnum == '' || $AR_name == '' || $SSL_name == '' || $SS_floor == '' || $RY_number == '' || $RY_bed == '' || $RY_wardrobe == '') {
        //         $return = array('res' => '40002', 'msg' => '请输入必填字段');
        //         $result = json_encode($return);
        //     } else {
        //         $query = $this->db->get_where('dorm_sslmanage', array('AR_name' => $AR_name, 'SSL_name' => $SSL_name));
        //         $data = array(
        //             'AR_number' => $query->row(0)->AR_number,
        //             'SSL_number' => $query->row(0)->SSL_number,
        //             'SQ_BG_SS_number' => $RY_number,
        //             'SQ_BG_bed' => $RY_bed,
        //             'SQ_BG_date' => $SQ_BG_date,
        //         );

        //         $data1 = array(
        //             'AR_number' => $query->row(0)->AR_number,
        //             'SSL_number' => $query->row(0)->SSL_number,
        //             'RY_number' => $RY_number,
        //             'RY_bed' => $RY_bed,
        //             'RY_wardrobe' => $RY_wardrobe
        //         );
        //         $this->db->where('SQ_RY_idcardnum', $RY_idcardnum);
        //         $this->db->update('dorm_stay_retreat_tune', $data);
        //         $return = $this->db->affected_rows();
        //         $this->db->where('RY_idcardnum', $RY_idcardnum);
        //         $this->db->update('dorm_livepeople', $data1);
        //         if ($return) {
        //             $return_su = array('res' => '40000', 'msg' => '宿舍调换成功');
        //             $result = json_encode($return_su);
        //         } else {
        //             $return_fa = array('res' => '40001', 'msg' => '宿舍调换失败');
        //             $result = json_encode($return_fa);
        //         }
        //     }
        //     return $result;
        // }

        //调宿方法
        public function tune_in($Coordinating_date ,$RY_name ,$RY_degsonid ,$RY_sex ,$RY_department ,$RY_check_in_address ,$AR_number ,$SSL_number ,$SS_number ,$RY_bed ,$RY_remarks)
        {
            if($Coordinating_date == '' || $RY_name == '' || $RY_degsonid == '' || $RY_sex == '' || $RY_department == '' || $RY_check_in_address == '' || $AR_number == '' || $SSL_number == '' || $SS_number == '' || $RY_bed == '')
            {
                $return = array('res' => '40002', 'msg' => '请输入必填字段' ,'data' => '');
                $result = json_encode($return);
            }else{
                $query_dorm_manday_calc_se = $this->db->get_where('dorm_manday_calc' ,array('RY_name' => $RY_name ,'RY_degsonid' => $RY_degsonid));
                if($query_dorm_manday_calc_se->num_rows()>1){
                //调宿日期正确性校验
                //若数据大于一条则取调宿日期最近的日期进行判断，若调宿日期-上一次调宿日期<0则报错
                $sj_dorm_manday_calc_se = "RY_name LIKE '%".$RY_name."%' AND RY_degsonid LIKE '%".$RY_degsonid."%' ORDER BY Coordinating_date DESC LIMIT 1";
                $sql_dorm_manday_calc_se = "SELECT * FROM dorm_manday_calc WHERE " .$sj_dorm_manday_calc_se;
                $query_dorm_manday_calc_se1 = $this->db->query($sql_dorm_manday_calc_se ,$sj_dorm_manday_calc_se);
                $start_Coordinating_date = new DateTime($query_dorm_manday_calc_se1->row()->Coordinating_date);
                $end_Coordinating_date = new DateTime($Coordinating_date);
                }elseif($query_dorm_manday_calc_se->num_rows()==1){
                    //若只有一条数据则取房间入住日期进行判断，若调宿日期-房间入住日期<0报错
                    $sj_dorm_manday_calc_se = "RY_name LIKE '%".$RY_name."%' AND RY_degsonid LIKE '%".$RY_degsonid."%'";
                    $sql_dorm_manday_calc_se = "SELECT * FROM dorm_manday_calc WHERE " .$sj_dorm_manday_calc_se;
                    $query_dorm_manday_calc_se1 = $this->db->query($sql_dorm_manday_calc_se ,$sj_dorm_manday_calc_se);
                    $start_Coordinating_date = new DateTime($query_dorm_manday_calc_se1->row()->Room_livedate);
                    $end_Coordinating_date = new DateTime($Coordinating_date);
                }else{;
                    //若一条数据都没有，则取人员信息的入住日期进行判断，若调宿日期-入住日期<0报错
                    $sj_dorm_manday_calc_se = "RY_name LIKE '%".$RY_name."%' AND RY_degsonid LIKE '%".$RY_degsonid."%'";
                    $sql_dorm_manday_calc_se = "SELECT * FROM dorm_livepeople WHERE " .$sj_dorm_manday_calc_se;
                    $query_dorm_manday_calc_se1 = $this->db->query($sql_dorm_manday_calc_se ,$sj_dorm_manday_calc_se);
                    $start_Coordinating_date = new DateTime($query_dorm_manday_calc_se1->row()->RY_live_date);
                    $end_Coordinating_date = new DateTime($Coordinating_date);
                }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
                $live_days = $start_Coordinating_date->diff($end_Coordinating_date)->days;
                if($live_days<0){
                    $return = array('res' => '40001', 'msg' => '请检查调宿日期' ,'data' => '');
                    $result = json_encode($return);
                };
                $SS_floor = substr($SS_number ,0 ,1);//楼层号
                $RY_check_in_address = $AR_number.'*'.$SS_floor.'*'.$SS_number.'*'.$RY_bed;
                $date_now = date("Y-m-d");
                //先将人员的房间信息取出
                $select_livepeople = array('AR_number' ,'SSL_number' ,'RY_number' ,'RY_live_date');
                $where_livepeople_old = array('RY_name' => $RY_name ,'RY_degsonid' => $RY_degsonid);
                $this->db->select($select_livepeople);
                $this->db->where($where_livepeople_old);
                $query_livepeople_old = $this->db->get('dorm_livepeople');

                //更新入住人员信息表
                $data_livepeople = array(
                    'AR_number' => $AR_number,                     //区域编号
                    'SSL_number' => $SSL_number,                   //宿舍楼编号
                    'RY_number' => $SS_number,                     //宿舍房间号
                    'SS_floor' => $SS_floor,                       //宿舍楼层号
                    'RY_check_in_address' => $RY_check_in_address, //入住位置
                    'RY_bed' => $RY_bed,                           //床位号
                    'RY_tune_in_check' => 'X',                     //是否已调宿
                    'RY_tune_in_date' => $Coordinating_date        //调宿日期
                );
                $where_livepeople_new_update = array('RY_name' => $RY_name ,'RY_degsonid' => $RY_degsonid);
                $this->db->where($where_livepeople_new_update);
                $this->db->update('dorm_livepeople' ,$data_livepeople);
                $query_livepeople = $this->db->affected_rows();
                //更新床位表
                //先取床位表人员信息
                $this->db->select('*');
                $query_bed_data = $this->db->get('dorm_bed');
                //将调宿的人员先从这个床位退宿
                $data_bed_old = array(
                    'RY_name' => '',      //人员姓名
                    'RY_sex' => '',       //性别
                    'RY_idcardnum' => '', //身份证号
                    'RY_degsonid' => '',  //工号
                    'RY_department' => '',//部门
                    'RY_phone' => '',     //联系方式
                    'RY_whether' => 0,    //是否住人
                    'RY_remarks' => ''    //备注
                );
                //更新
                $where_bed_old = array('RY_name' => $RY_name ,'RY_degsonid' => $RY_degsonid);
                $this->db->where($where_bed_old);
                $this->db->update('dorm_bed' ,$data_bed_old);
                $query_bed_old = $this->db->affected_rows();

                //再将这个人员入住到新的床位
                $where_bed = array('AR_number' => $AR_number ,'SSL_number' => $SSL_number ,'SS_number' => $SS_number ,'RY_bed' => $RY_bed );
                $data_bed_new = array(
                    'RY_name' => $RY_name,                                  //人员姓名
                    'RY_sex' => $RY_sex,                                    //性别
                    'RY_idcardnum' => $query_bed_data->row()->RY_idcardnum, //身份证号
                    'RY_degsonid' => $RY_degsonid,                          //工号
                    'RY_department' => $RY_department,                      //部门
                    'RY_phone' => $query_bed_data->row()->RY_phone,         //联系方式
                    'RY_whether' => 1,                                      //床位是否住人
                );
                //重新分配床位
                $this->db->where($where_bed);
                $this->db->update('dorm_bed' ,$data_bed_new);
                $query_bed_new = $this->db->affected_rows();

                //将调宿记录插入记录表
                //先将人员信息取出来
                // $where_livepeople = array('RY_name' => $RY_name ,'RY_degsonid' => $RY_degsonid);
                // $this->db->select('*');
                // $this->db->where($where_livepeople);
                // $query_livepeople_data = $this->db->get('dorm_livepeople');
                $query_livepeople_data = $this->db->get_where('dorm_livepeople' ,array('RY_name' => $RY_name ,'RY_degsonid' => $RY_degsonid));
                // try{
                    $data_tune_in = array(
                        'AR_number' => $AR_number,                                               //区域编号
                        'SSL_number' => $SSL_number,                                             //宿舍楼编号
                        'SS_floor' => $SS_floor,                                                 //宿舍楼层号
                        'SQ_SS_number' => $SS_number,                                            //宿舍房间号
                        'SQ_RY_idcardnum' => $query_livepeople_data->row()->RY_idcardnum,        //身份证号
                        'SQ_RY_number' => $RY_degsonid,                                          //工号
                        'SQ_RY_name' => $RY_name,                                                //人员姓名
                        'SQ_RY_department' => $RY_department,                                    //部门
                        'SQ_RY_job' => $query_livepeople_data->row()->RY_job,                    //职位
                        'SQ_RY_sex' => $RY_sex,                                                  //性别
                        'SQ_RY_phone' => $query_livepeople_data->row()->RY_phone,                //联系方式
                        'SQ_RY_location' => $query_livepeople_data->row()->RY_location,          //籍贯
                        'SQ_RY_address' => $query_livepeople_data->row()->RY_address,            //户籍地址
                        'SQ_type' => 2,                                                          //0:退宿 1:入住 2:调宿 类别
                        'SQ_date' => $date_now,                                                  //申请日期
                        'SQ_bed' => $RY_bed,                                                     //床位号
                        'SQ_wardrobe' => $query_livepeople_data->row()->RY_wardrobe,             //柜子号
                        'SQ_reason' => $RY_remarks,                                              //备注
                        'SQ_live_date' => $query_livepeople_data->row()->RY_live_date,           //入住日期
                        'SQ_liaison' => $query_livepeople_data->row()->RY_emergency_contact,     //紧急联系人
                        'SQ_relationship' => $query_livepeople_data->row()->RY_relationship,     //与紧急联系人关系
                        'SQ_liaison_phone' => $query_livepeople_data->row()->RY_emergency_phone, //紧急联系人联系方式
                        'SQ_BG_SS_number' => $SS_number,                                         //变更房号
                        'SQ_BG_bed' => $RY_bed,                                                  //变更床位
                        'SQ_BG_date' => $Coordinating_date,                                               //变更日期
                        'SQ_remarks' => $RY_remarks,                                             //备注
                        'SQ_applicant' => $RY_name                                               //申请人
                    );
                // }catch(Exception $e){
                    // echo 'Message:'.$e->getMessage();
                // }
                //将数据插入记录表
                $this->db->insert('dorm_stay_retreat_tune' ,$data_tune_in);
                $query_tune = $this->db->affected_rows();

                //更新宿舍房间表
                //更新原宿舍房间表
                //取原宿舍的实际人数和剩余可住人数
                $select_livepeople_old = array('SS_peoplenumber' ,'SS_remain_people');
                $where_livepeople_old = array('AR_number' => $query_livepeople_old->row()->AR_number ,'SSL_number' => $query_livepeople_old->row()->SSL_number ,'SS_number' => $query_livepeople_old->row()->RY_number);
                $this->db->select($select_livepeople_old);
                $this->db->where($where_livepeople_old);
                $query_old_roominfor = $this->db->get('dorm_roominfor');
                $SS_peoplenumber_old_Weniger = $query_old_roominfor->row()->SS_peoplenumber - 1;//原宿舍的宿舍实际人数-1
                $SS_remain_people_old_add = $query_old_roominfor->row()->SS_remain_people + 1;//原宿舍的宿舍剩余可住+1

                $data_old_roominfor = array(
                    'SS_peoplenumber' => $SS_peoplenumber_old_Weniger,//当前宿舍的实际人数
                    'SS_remain_people' => $SS_remain_people_old_add   //当前宿舍的剩余可住人数
                );
                //更新原宿舍实际人数,剩余可住人数
                $this->db->where($where_livepeople_old);
                $this->db->update('dorm_roominfor' ,$data_old_roominfor);
                $query_roominfor_old = $this->db->affected_rows();
                
                //取更换后的宿舍的宿舍实际人数,剩余可住人数
                $select_livepeople_new = array('SS_peoplenumber' ,'SS_remain_people');
                $where_livepeople_new = array('AR_number' => $AR_number ,'SSL_number' => $SSL_number ,'SS_number' => $SS_number);
                $this->db->select($select_livepeople_new);
                $this->db->where($where_livepeople_new);
                $query_new_roominfor = $this->db->get('dorm_roominfor');
                $SS_peoplenumber_new_add = $query_new_roominfor->row()->SS_peoplenumber + 1;//调换的宿舍实际人数+1
                $SS_remain_people_new_Weniger = $query_new_roominfor->row()->SS_remain_people -1;//调换的宿舍剩余可住人数-1

                $data_new_roominfor = array(
                    'SS_peoplenumber' => $SS_peoplenumber_new_add,       //当前宿舍的实际人数
                    'SS_remain_people' => $SS_remain_people_new_Weniger  //当前宿舍的剩余可住人数
                );

                //更新调换宿舍实际人数，剩余可住人数
                $this->db->where($where_livepeople_new);
                $this->db->update('dorm_roominfor' ,$data_new_roominfor);
                $query_roominfor_new = $this->db->affected_rows();

                //更新水电人天表
                //先将调宿日期更新
                //若水电表一条数据都没有，则先插入一条
                $where_Coordinating_date = array(
                    'RY_name' => $RY_name,
                    'RY_degsonid' => $RY_degsonid 
                );
                $query_Coordinating_data = $this->db->get_where('dorm_manday_calc' ,$where_Coordinating_date);
                if($query_Coordinating_data->num_rows() == 0){
                    $data_Coordinating_date_new = array(
                    'AR_number' => $query_livepeople_old->row()->AR_number,       //区域编号
                    'SS_number' => $query_livepeople_old->row()->SSL_number,      //宿舍楼编号
                    'SS_room_number' => $query_livepeople_old->row()->RY_number,  //宿舍房间号
                    'RY_name' => $RY_name,                                        //人员姓名
                    'RY_degsonid' => $RY_degsonid,                                //工号
                    'SS_livedate' => $query_livepeople_old->row()->RY_live_date,  //宿舍入住日期
                    'Room_livedate' => $query_livepeople_old->row()->RY_live_date //房间入住日期
                );
                $this->db->insert('dorm_manday_calc' ,$data_Coordinating_date_new);
                }

                $data_Coordinating_date = array(
                    'Coordinating_date' => $Coordinating_date
                );
                // $where_manday_calc = array('RY_name' => $RY_name ,'RY_degsonid' => $RY_degsonid ,'Coordinating_date' => '');
                // $this->db->where($where_manday_calc);
                // $this->db->update('dorm_manday_calc' ,$data_Coordinating_date);
                
                //调宿日期更新
                $sj_dorm_manday_calc = "RY_name LIKE '%".$RY_name."%' AND RY_degsonid LIKE '%".$RY_degsonid."%' AND Coordinating_date IS NULL";
                $sql_dorm_manday_calc = "UPDATE dorm_manday_calc SET Coordinating_date = '".$Coordinating_date."' WHERE " .$sj_dorm_manday_calc;
                $this->db->query($sql_dorm_manday_calc ,$sj_dorm_manday_calc);
                $query_Coor_update = $this->db->affected_rows();

                //计算所住房间的居住天数
                // $query_dorm_manday_calc_se = $this->db->get_where('dorm_manday_calc' ,array('RY_name' => $RY_name ,'RY_degsonid' => $RY_degsonid));
                // //若有多条数据则取调宿日期最近的那条数据进行更新
                if($query_dorm_manday_calc_se->num_rows()>1){
                    // $sj_dorm_manday_calc_se = "RY_name LIKE '%".$RY_name."%' AND RY_degsonid LIKE '%".$RY_degsonid."%' ORDER BY Coordinating_date DESC LIMIT 1";
                    // $sql_dorm_manday_calc_se = "SELECT * FROM dorm_manday_calc WHERE " .$sj_dorm_manday_calc_se;
                    // $query_dorm_manday_calc_se1 = $this->db->query($sql_dorm_manday_calc_se ,$sj_dorm_manday_calc_se);
                    // $start_Coordinating_date = new DateTime($query_dorm_manday_calc_se1->row()->Coordinating_date);
                    // $end_Room_livedate = new DateTime($query_dorm_manday_calc_se1->row()->Room_livedate);
                    // $live_days = $start_Coordinating_date->diff($end_Room_livedate)->days;
                    //若有多条取最近的一条调宿日期的数据的调宿日期与房间入住日期比较，若同年同月则入住天数 = 调宿日期 - 房间入住日期 ，若不同则调宿日期 - 当年当月的第一天
                    //取最近的一条调宿日期的数据的年份
                    $year = substr($query_dorm_manday_calc_se1->row()->Room_livedate ,0 ,4);
                    //取最近的一条调宿日期的数据的月份
                    $month = substr($query_dorm_manday_calc_se1->row()->Room_livedate ,5 ,2);
                    if($year == date("Y") && $month == date("m")){
                        $start_Coordinating_date1 = new DateTime($query_dorm_manday_calc_se1->row()->Coordinating_date);
                        $end_room_livedate = new DateTime($query_dorm_manday_calc_se1->row()->Room_livedate);
                        $live_days = $start_Coordinating_date1->diff($end_room_livedate)->days;
                    }else{
                        $start_Coordinating_date1 = new DateTime($query_dorm_manday_calc_se1->row()->Coordinating_date);
                        $start_month = date("Y") ."-" .date("m"). "-" ."01";
                        $end_month = new DateTime($start_month);
                        $live_days = $start_Coordinating_date1->diff($end_month)->days;
                    }
                    //将入住天数进行更新
                    $sql_dorm_manday_calc_up = "UPDATE dorm_manday_calc SET live_days = '".$live_days."' WHERE " .$sj_dorm_manday_calc_se;
                    $this->db->query($sql_dorm_manday_calc_up ,$sj_dorm_manday_calc_se);
                    $query_manday_calc_up = $this->db->affected_rows();
                //若只有一条数据则将这条数据的入住天数更新
                }else{
                    //若只有一条取这条数据的调宿日期与房间入住日期比较，若同年同月则入住天数 = 调宿日期 - 房间入住日期 ，若不同则调宿日期 - 当年当月的第一天
                    //取最近的一条调宿日期的数据的年份
                    $year = substr($query_dorm_manday_calc_se1->row()->Room_livedate ,0 ,4);
                    //取最近的一条调宿日期的数据的月份
                    $month = substr($query_dorm_manday_calc_se1->row()->Room_livedate ,5 ,2);
                    if($year == date("Y") && $month == date("m")){
                        $start_Coordinating_date1 = new DateTime($query_dorm_manday_calc_se1->row()->Coordinating_date);
                        $end_room_livedate = new DateTime($query_dorm_manday_calc_se1->row()->Room_livedate);
                        $live_days = $start_Coordinating_date1->diff($end_room_livedate)->days;
                    }else{
                        $start_Coordinating_date1 = new DateTime($query_dorm_manday_calc_se1->row()->Coordinating_date);
                        $start_month = date("Y") ."-" .date("m"). "-" ."01";
                        $end_month = new DateTime($start_month);
                        $live_days = $start_Coordinating_date1->diff($end_month)->days;
                    }
                    // $start_Coordinating_date = new DateTime($query_dorm_manday_calc_se->row()->Coordinating_date);
                    // $end_Room_livedate = new DateTime($query_dorm_manday_calc_se->row()->Room_livedate);
                    // $live_days = $start_Coordinating_date->diff($end_Room_livedate)->days;
                    $manday_calc_up = array(
                        'live_days' => $live_days 
                    );
                    $this->db->where($where_livepeople_new_update);
                    $this->db->update('dorm_manday_calc' ,$manday_calc_up);
                    $query_manday_calc_up = $this->db->affected_rows();
                }

                //再插入一条数据
                //先取最后一条数据的调宿日期
                $sql_dorm_manday_calc_in = "SELECT Coordinating_date FROM dorm_manday_calc WHERE RY_name LIKE '%".$RY_name."%' AND RY_degsonid LIKE '%".$RY_degsonid."%' ORDER BY Coordinating_date DESC LIMIT 1";
                $query_Coordinating_date = $this->db->query($sql_dorm_manday_calc_in);
                $data_manday_calc = array(
                    'AR_number' => $AR_number,
                    'SS_number' => $SSL_number,
                    'SS_room_number' => $SS_number,
                    'RY_name' => $RY_name,
                    'RY_degsonid' => $RY_degsonid,
                    'SS_livedate' => $query_livepeople_old->row()->RY_live_date,
                    'Room_livedate' => $query_Coordinating_date->row()->Coordinating_date
                );
                //数据插入
                $this->db->insert('dorm_manday_calc' ,$data_manday_calc);
                $query_manday_calc = $this->db->affected_rows();

                // print_r($query_livepeople);
                // print_r($query_bed_old);
                // print_r($query_bed_new);
                // print_r( $query_tune);
                // print_r($query_roominfor_old);
                // print_r($query_roominfor_new);
                // print_r($query_Coor_update);
                // print_r($query_manday_calc);
                // print_r($query_manday_calc_up);
                // return;
                if($query_livepeople && $query_bed_old && $query_bed_new && $query_tune && $query_roominfor_old && $query_roominfor_new && $query_Coor_update && $query_manday_calc && $query_manday_calc_up){
                    $return_su = array('res' => '40000', 'msg' => '宿舍调换成功');
                    $result = json_encode($return_su);
                }else{
                    $return_fa = array('res' => '40001', 'msg' => '宿舍调换失败');
                    $result = json_encode($return_fa);
                }
            }
            return $result;
        }
    }
